<template>
  <div>
    <div class="top-panel">
      <el-card>
        <el-form label-width="40px" label-position="right">
          <el-row :gutter="10">
            <el-col :span="3">
              <el-form-item label="标题">
                <el-input
                    v-model="searchForm.titleFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter.native="loadDataList">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="分类">
                <CategorySelect
                    :style="{width:'100%'}"
                    :type="0"
                    v-model="searchForm. categoryId"
                    @change="loadDataList"
                ></CategorySelect>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="难度">
                <el-select
                    clearable
                    placeholder="请选择难度"
                    v-model="searchForm.difficultyLevel"
                    @change="loadDataList"
                >
                  <el-option :value="1" label="一星"></el-option>
                  <el-option :value="2" label="二星"></el-option>
                  <el-option :value="3" label="三星"></el-option>
                  <el-option :value="4" label="四星"></el-option>
                  <el-option :value="5" label="五星"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="状态">
                <el-select
                    clearable
                    placeholder="请选择状态"
                    v-model="searchForm.status"
                    @change="loadDataList"
                >
                  <el-option :value="0" label="待发布"></el-option>
                  <el-option :value="1" label="已发布"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="创建人" label-width="60px">
                <el-input
                    v-model="searchForm.createUserNameFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter.native="loadDataList">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7" :style="{paddingLeft:'10px'}">
              <el-button-group>
                <el-button type="success" @click="loadDataList()">查询</el-button>
                <el-button type="primary" @click="showEdit" v-has="proxy.PermissionCode.question.edit">新增问题</el-button>
                <el-button type="primary" @click="importData" v-has="proxy.PermissionCode.question.import">批量导入
                </el-button>
                <el-button type="primary" @click="postQuestionBatch()" v-has="proxy.PermissionCode.question.post"
                           :disabled="selectRowList.length===0">批量发布
                </el-button>
                <el-button type="danger" @click="delQuestionBatch()" v-has="proxy.PermissionCode.question.del_batch"
                           :disabled="selectRowList.length===0">批量删除
                </el-button>
              </el-button-group>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>

    <!-- 内容区域 -->
    <el-card :style="{ height: 'calc(100vh - 192px)',marginTop:'10px'}">
      <template #header>
        <span>问题列表</span>
      </template>
      <Table
          ref="tableInfoRef"
          :columns="columns"
          :fetch="loadDataList"
          :dataSource="tableData"
          :options="tableOptions"
          :extHeight="tableOptions.extHeight"
          :selected="selectedHandler"
          @rowSelected="rowSelected"
      >
        <template #slotTitle="{ index,row }">
          {{ row.title }}
        </template>
        <template #slotDifficultyLevel="{ index,row }">
          <el-rate v-model="row.difficultyLevel" disabled></el-rate>
        </template>
        <template #slotStatus="{ index,row }">
          <div>{{ row.status === 1 ? '已发布' : '待发布' }}</div>
        </template>

        <template #slotOperation="{ index,row }">
          <div class="row-op-panel">
            <a
                class="a-link"
                href="javascript:void(0)"
                @click="showEdit(row)"
                v-has="proxy.PermissionCode.question.edit"
                v-if="(row.status !== 1 && parseInt(row.createUserId) === userInfo.userId) || userInfo.superAdmin"
            >修改</a>
            <!--  v-has="proxy.PermissionCode.role.edit"
             v-has="proxy.PermissionCode.role.del" -->
            <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="delQuestion(row)"
                v-has="proxy.PermissionCode.question.del"
                v-if="(row.status !== 1 && parseInt(row.createUserId) === userInfo.userId)|| userInfo.superAdmin"
            >删除</a>
            <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="postQuestion(row)"
                v-has="proxy.PermissionCode.question.post"
            >{{ row.status === 1 ? '取消发布' : '发布' }}</a>
          </div>
        </template>
      </Table>
    </el-card>
    <QuestionEdit ref="questionEditRef" @reload="loadDataList"></QuestionEdit>
  </div>
</template>


<script setup>
import {onMounted, ref, getCurrentInstance} from "vue";
import QuestionEdit from "./QuestionEdit.vue"

const {proxy} = getCurrentInstance();
const userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
const api = {
  loadDataList: "/questionInfo/loadDataList",
  delQuestion: "/questionInfo/delQuestion",
  delQuestionBatch: "/questionInfo/delQuestionBatch",
  postQuestion: "/questionInfo/postQuestion",
  cancelPostQuestion: "/questionInfo/cancelPostQuestion"
}

const searchForm = ref({})

const tableData = ref({});
const tableOptions = ref({
  extHeight: 125,
  selectType: "checkbox"
})

const columns = [
  {
    label: "ID",
    prop: "questionId",
    width: 80
  },
  {
    label: "标题",
    prop: "title",
    scopedSlots: "slotTitle"
  },
  {
    label: "分类",
    prop: "categoryName",
    width: 150,
  },
  {
    label: "难度",
    prop: "difficultyLevel",
    width: 130,
    scopedSlots: "slotDifficultyLevel",
  },
  {
    label: "创建人",
    prop: "createUserName",
    width: 150
  },
  {
    label: "创建时间",
    prop: "createTime",
    width: 180
  },
  {
    width: 90,
    label: "发布状态",
    prop: "status",
    scopedSlots: "slotStatus"
  },
  {
    width: 180,
    label: "操作",
    scopedSlots: "slotOperation"
  },
]

const tableInfoRef = ref();

const loadDataList = async () => {
  let params = {
    pageNo: tableData.value.pageNo,
    pageSize: tableData.value.pageSize,
    ...searchForm.value
  }
  let result = await proxy.Request({
    url: api.loadDataList,
    params: params,
  })
  if (!result) {
    return;
  }
  tableData.value = result.data;
}


// 修改状态
const editStatus = (row) => {
  let status = row.status === 1 ? '0' : '1';
  let info = status === '1' ? '禁用' : '启用'
  proxy.Confirm(`你确定要${info} [用户${row.userName}] 吗？`, async () => {
    let result = await proxy.Request({
      url: api.updateStatus,
      params: {
        userId: row.userId,
        status
      },
    })
    if (!result) {
      return;
    }
    proxy.Message.success("修改用户状态成功");
    // 改变数据
    loadDataList();
  })
}
// 批量导入
const importData = () => {
  alert("未开发")
}

// 选中方法
const selectedHandler = (row) => {
  // 未发布才可选中
  return row.status === 0;
}
// 已经选中
const selectRowList = ref([]);
const rowSelected = (rows) => {
  selectRowList.value = rows.map(item => item.questionId);
}

// 删除问题
const delQuestion = (data) => {
  proxy.Confirm(`你确定要删除问题【${data.title}】吗？`, async () => {
    let result = await proxy.Request({
      url: api.delQuestion,
      params: {
        questionId: data.questionId
      },
    })
    if (!result) {
      return;
    }
    proxy.Message.success("删除成功");
    loadDataList();
  })
}
// 批量删除
const delQuestionBatch = () => {
  proxy.Confirm(`你确定要批量删除这${selectRowList.value.length}个问题吗？`, async () => {
    let result = await proxy.Request({
      url: api.delQuestionBatch,
      params: {
        questionIds: selectRowList.value
      },
    })
    if (!result) {
      return;
    }
    proxy.Message.success("批量删除成功");
    loadDataList();
  })
}

// 发布或取消发布问题
const postQuestion = async (row) => {
  let result = await proxy.Request({
    url: row.status === 1 ? api.cancelPostQuestion : api.postQuestion,
    params: {
      questionIds: row.questionId
    },
  })
  if (!result) {
    return;
  }
  proxy.Message.success("修改发布状态成功")
  loadDataList();
}
// 批量发布
const postQuestionBatch = async () => {
  let result = await proxy.Request({
    url: api.postQuestion,
    params: {
      questionIds: selectRowList.value
    },
  })
  if (!result) {
    return;
  }
  proxy.Message.success("批量发布成功")
  loadDataList();
}

const questionEditRef = ref();
// 编辑问题
const showEdit = (data = {}) => {
  questionEditRef.value.showEdit(Object.assign({}, data))
}


</script>


<style scoped lang="scss">
.box-card {
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .tree-panel {
    overflow: auto;
    height: calc(100vh - 304px);
  }
}
</style>
